﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment.Admin
{
    public partial class userMng : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindList();
            }
            if (Session["LoginUser"] == null)    //上面Session名字用LoginUser,这里的名字要和上面的相同,可以随便改,保持一致就行了,注意:Session["LoginUser"]后面,没有ToString()哦,你要是加上的话,就会出错了.
            {
                Response.Redirect("logon.aspx"); //如果Session为空,则转到登陆页.
            }
        }

        DS data = new DS();  //建立数据库连接
        protected void queryAll_Click(object sender, EventArgs e)
        {
            bindList();  //绑定源
        }

        protected void query_Click(object sender, EventArgs e)
        {
            string strName = this.name.Text.Trim();
            string strSql = String.Format("SELECT *FROM UserInfo WHERE Name='{0}'", strName);
            DataTable table = data.GetDataSource(strSql, "Query");
            if (table.Rows.Count==0)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('请填写完整信息！');</script>");
                return;
            }
            else
            {
                bindList(strSql); //查询特定人员信息
            }
        }

        protected void bindList()
        {
            string strSql = "SELECT *FROM UserInfo";
            DataTable table = data.GetDataSource(strSql, "Query");
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();

        }
        protected void bindList(string sql)
        {
            DataTable table = data.GetDataSource(sql, "Query");
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strID = (this.GridView1.Rows[e.RowIndex].FindControl("ID") as Label).Text.Trim();
            string strSql = String.Format("DELETE FROM UserInfo WHERE User_ID='{0}'", strID);
            if (data.ExcuteSql(strSql))
            {
                bindList();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('删除失败！');</script>");
                return;
            }
        }
    }
}
